// pages/department/department.js
const utils = require('../../utils/util')
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    department: [],
    dHeight: ''
  },
  getDepartment() {
    let that = this;
    utils.getAjax(`${app.globalData.apiUrl}/Api/Food/get_food_content`, res=> {
      that.setData({
        department: res.data
      })
    }, {status: 1})
  },
  toAdd(e) {
    let type = e.currentTarget.dataset.type;
    wx.navigateTo({
      url: '/pages/departmentInfo/departmentInfo?type=' + type
    })
  },
  slideButtonTap(e) {
    let that = this;
    // console.log('slide button tap', e.detail, e.currentTarget)
    if (e.detail.data.type === 1) {
      // 修改部门信息
      wx.navigateTo({
        url:'/pages/departmentInfo/departmentInfo?type=2&departmentid=' + e.currentTarget.dataset.did + '&contentname=' + e.currentTarget.dataset.name
      })
    } else if(e.detail.data.type === 2) {
      // 删除部门
      let delId = e.currentTarget.dataset.did
      wx.showModal({
        title:'提示',
        content:'是否确认删除部门 ' + e.currentTarget.dataset.name + ' ?',
        success(res){
            if(res.confirm){
                utils.getAjax(`${app.globalData.apiUrl}/Api/Food/operation_food_content`,res=>{
                    console.log(res)
                    if(res.data.code == 1){
                        utils.showToast('部门删除成功','success',1000);
                        that.getDepartment();
                    }
                },{
                    type: 3,
                    id:delId,
                    status: 2
                })
            }
        }
      })
    }
  },
  // 获取屏幕高度
  getScreen() {
    let that = this;
   wx.getSystemInfo({
     success: function(res) {
      that.setData({
        dHeight: res.windowHeight
      })
     }
   })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    utils.setNavBarTitle('部门管理')
    this.setData({
      slideButtons: [{
        text: '修改',
        data: {type: 1}
      },{
        type: 'warn',
        text: '删除',
        data: {type: 2}
      }]
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getDepartment()
    this.getScreen()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})